package cn.tedu.demo4.mapper;

import cn.tedu.demo4.pojo.UserInfo;
import cn.tedu.demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 只负责数据库的增删改查操作
 */
@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbutil;
    @Override
    public int insert(UserInfo userInfo) {
        PreparedStatement ps = null;
        Connection conn = null;
        System.out.println("UserMapper:插入User表");
        try {
            conn = dbutil.getConnection();
            String sql = "insert into userinfo(username,password,nickname,age)" +
                    "values(?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,userInfo.getUsername());
            ps.setString(2,userInfo.getPassword());
            ps.setString(3,userInfo.getNickname());
            ps.setInt(4,userInfo.getAge());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        /*
        1.获取数据库连接
        2.创建Connection对象
        3.创建PreparedStatement对象
        4.执行SQL语句
        5.接收数据库处理结果
        6.关闭数据库链接
        7.返回结果
         */
        return 0;
    }

    @Override
    public UserInfo selectByName(String username) {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        UserInfo userInfo = null;
        try {
            conn = dbutil.getConnection();
            String sql = "select id,username,password,nickname,age " +
                    "from userinfo " +
                    "where username = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            rs = ps.executeQuery();
            if (rs.next()) {
                userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                return userInfo;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 1.连接数据库
         * 2:执行查询语句
         * SELECT id, username, password,nickname, age FROM userinfo
         * WHERE username=?3:执行得到一个查询结果集
         * 4:将结果集中查询出来的该用户信息封装成UserInfo对象
         * 例如:
         * if(rs.next()){
         * Userinfo userinfo = new Userinfo();
         * //把查询到的这条记录中个字段的值设置到userinfo对象的对应属性上 userinfo.setUsername(rs.getString("username"))
         * ...
         * return userinfo;
         * }
         */
        return null;
    }

    @Override
    public List<UserInfo> selectAll() {
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection conn = null;
        List<UserInfo> userList = new ArrayList<>();
        try {
            conn = dbutil.getConnection();
            String sql = "select * from userinfo ";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                UserInfo userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                userList.add(userInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ps != null){
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (rs != null){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return userList;
    }

}
